using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FCISPlus.Client.Communicator;

namespace FCISPlus.Client.UI
{
    public partial class Feeds : Form
    {
        /// <summary>
        /// The client communicator.
        /// </summary>
        private IFCISPlusClientCommunicator communicator;

        /// <summary>
        /// The current User object
        /// </summary>
        private FCISPlus.Server.Database.User mySelf;

        public Feeds(IFCISPlusClientCommunicator communicator)
        {
            InitializeComponent();
            this.communicator = communicator;
        }

        private void Feeds_Load(object sender, EventArgs e)
        {
            mySelf = communicator.GetUserProfile();
            ReloadFeedControl();
        }

        private void ReloadFeedControl()
        {
            lvFeed.Items.Clear();
            FCISPlus.Server.Database.Post[] posts = this.communicator.GetFeed();

            foreach (FCISPlus.Server.Database.Post post in posts)
            {                
                //inserting the feed post as {poster: post text, time since post, # of likes, #of comments}
                
                      // note: if you don't understand the "Invoke( (Func<string>)" code below, you don't have to
                      //, just format the time and likes strings in the usual way
                      // I was just experimenting "inline automatically exectuted lamda statments", but you don't have to do that !
                
                ListViewItem lvi = lvFeed.Items.Add(new ListViewItem(
                    new string[] {                     
                        post.User.Username + ": " + post.PostText                    
                    
                        , Invoke( (Func<string>) ( () => {
                                                            TimeSpan s = DateTime.Now.Subtract(post.PostDate);
                                                            string timeSincePost = "";
                                                            if(s.Days != 0) timeSincePost = s.Days + " days ago";
                                                            else if(s.Hours != 0) timeSincePost = s.Hours + " hours ago";
                                                            else if(s.Minutes != 0) timeSincePost = s.Minutes + " minutes ago";
                                                            else timeSincePost = s.Seconds + " seconds ago";                         
                                                            return timeSincePost;
                                                        } ) ).ToString()                    
                    
                        , Invoke( (Func<string>) ( () => {
                                                            string likesString = "";
                                                            if(post.Likes.Count == 1) likesString = "1 Like";
                                                            else if(post.Likes.Count > 1 ) likesString = post.Likes.Count + " Likes";
                                                            return likesString;
                                                          } ) ).ToString()

                        , Invoke( (Func<string>) ( () => {
                                                            string numCommetsString = "";
                                                            if(post.Comments.Count == 1) numCommetsString = "1 Comment";
                                                            else if(post.Comments.Count > 1 ) numCommetsString = post.Comments.Count + " Comments";
                                                            return numCommetsString;
                                                          } ) ).ToString()
 
                    }));

                lvi.Tag = post;
            }
        }

        private void btnLike_Click(object sender, EventArgs e)
        {
            if (lvFeed.SelectedItems.Count == 1)
            {
                ListViewItem lvi = lvFeed.SelectedItems[0];
                FCISPlus.Server.Database.Post post = (FCISPlus.Server.Database.Post)lvi.Tag;
                FCISPlus.Server.Database.Like[] likes = post.Likes.Where(x => x.UserID == mySelf.UserID).ToArray();

                if (likes.Length == 0)
                {
                    //then we should like
                    this.communicator.Like(post);
                }
                else
                {
                    //we should unlike
                    this.communicator.Unlike(post);
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ReloadFeedControl();
        }

        private void lvFeed_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvFeed.SelectedItems.Count == 0)
            {
                btnLike.Text = "Like";
            }
            else if (lvFeed.SelectedItems.Count == 1)
            {
                FCISPlus.Server.Database.Post post = (FCISPlus.Server.Database.Post)lvFeed.SelectedItems[0].Tag;
                FCISPlus.Server.Database.Like[] likes = post.Likes.Where(x => x.UserID == mySelf.UserID).ToArray();

                if (likes.Length == 1)
                {
                    btnLike.Text = "Unlike";
                }
                else
                {
                    btnLike.Text = "Like";
                }
            }
        }

        private void lvFeed_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lvFeed.SelectedItems.Count == 1)
            {
                FCISPlus.Server.Database.Post post = (FCISPlus.Server.Database.Post)lvFeed.SelectedItems[0].Tag;
                ViewAddCommentsForm commentsForm = new ViewAddCommentsForm(communicator, post);
                commentsForm.Show();
            }            
        }
    }
}
